function labClick(obj) {
	var v = $("#propertyKey").val();
	var hrefStr = "/synonymy/synonymy_list.htm?propertyKey=";
	if(v != $(obj).html()) {
		hrefStr += $(obj).html();
	}
	location.href = hrefStr;
}

function getList() {
	location.href = "/synonymy/synonymy_list.htm";
}

function deleteSynonymy(id) {
	if(confirm("确定要删除这个同义词吗?")) {
		API.synonymyDelete({id: id}, function(res) {
			if (res.code == '0') {
				getList();
			} else {
				alert(res.desc);
			}
		});
	}
}

function mergeSynonymy() {
	var idBoxes = $("input[name='id_box']:checked");
	// 合并至少需要两条勾选的数据
	if(idBoxes.length < 2) {
		alert("请至少选择两条数据进行合并");
		return false;
	}
	
	// 如果是小词同义词则需判断合并数据的属性必须一致
	var tabid = $("#tabid");
	if(tabid == '1_1') {
		var propertyArray = $(idBoxes[0]).parent().next().next().html();
		for(var i=1;i<idBoxes.length;i++) {
			if(propertyArray != $(idBoxes[i]).parent().next().next().html()) {
				alert("合并的同义词属性不一致");
				return false;
			}
		}
	}
	
	// 确定同步
	if(confirm("确定要合并吗？")) {
		API.synonymyMerge(idBoxes.serialize(), function(res) {
			if(res.code == '0') {
				getList();
			} else {
				alert(res.desc);
			}
		});
	}
}

function synonymyImport(obj) {
	if(UTIL.checkButton(obj)) {
		$("#file").click();
		UTIL.releaseButton(obj);
		return true;
	}
}

$("#file").change(fileChange);

function fileChange(){
	var file = $("#file").val();
	$("#importing").show();
	$("#import").hide();
	if(file) {
		var strFileName=file.replace(/^.+?\\([^\\]+?)(\.[^\.\\]*?)?$/gi,"$1");  //正则表达式获取文件名，不带后缀
		var FileExt=file.replace(/.+\./,"");   //正则表达式获取后缀
		
		var tabid = $("#tabid");
		// 判断文件名是否符合
		if(tabid == '21_7') {
			var typeArr = new Array();
			$(".lab-ico").each(function() {
				typeArr.push($(this).html());
			});
			var flag = false;
			for(var i in typeArr) {
				if(typeArr[i] == strFileName) {
					flag = true;
					break;
				}
			}
			if(!flag) {
				alert("文件名请用属性的一种！");
				return false;
			}
		}
		
		// 后缀只能是xlsx，其他不处理
		if(FileExt != 'xlsx') {
			alert("请上传xlsx后缀的Excel文件");
			return false;
		}
		
		$.ajaxFileUpload({
			url: '/synonymy/synonymy_import.do',
			secureuri: false,
			fileElementId: 'file',
			dataType: 'json',
			data: {"property" : strFileName},
			success: function(data) {
				$("#file").after($("#file").clone().val(""));
				$("#file").remove();
				$("#file").change(fileChange);
				if(data.code == '0') {
					alert("导入成功！");
					getList();
				} else {
					if(data.code == "40002") {
						alert(data.data.errorMsg);
					} else {
						alert(data.desc);
					}
					getList();
				}
			},
			error: function (data, e) {
				alert("导入失败！");
				$("#importing").hide();
				$("#import").show();
			}
		});
	}
}

function synonymyExport(obj, property, no) {
	if(no  && !property) {
		alert("请选择一种属性！");
		return;
	}
	if(UTIL.checkButton(obj)) {
		if(no && no == '2') {
			window.location.href="/synonymy/synonymy_export.htm?property=" + property + "&no=2";
		} else {
			window.location.href="/synonymy/synonymy_export.htm?property=" + property;
		}
		UTIL.releaseButton(obj);
	}
}

function changeStd(obj, id, word,property) {
	API.synonymyChangeStd({id: id, word:word,property:property}, function(res) {
		if (res.code == '0') {
			getList();
		} else {
			alert(res.desc);
			getList();
		}
	});
}

function changeEnglishStd(obj, id, word,property) {
	API.synonymyChangeEnglishStd({id: id, word:word,property:property}, function(res) {
		if (res.code == '0') {
			getList();
		} else {
			alert(res.desc);
			getList();
		}
	});
}

function submitSearch(obj) {
	var exactSearchFlag = 0;
	if($("#exactSearchFlag").prop("checked")) {
		exactSearchFlag = 1;
	}
	
	var wordKey = $("#wordKey");
	location.href = "/synonymy/synonymy_list.htm?exactSearchFlag=" + exactSearchFlag + "&wordKey=" + wordKey;
}

function elementTInit(obj) {
	if(UTIL.checkButton(obj)) {
		API.synonymyElementTInit(function(res) {
			if (res.code == '0') {
				alert("刷新成功！");
				window.location.reload();
			} else {
				alert(res.desc);
			}
		});
	}
}

function synonymyRInit(obj){
	if(UTIL.checkButton(obj)) {
		API.synonymyElementRInit(function(res) {
			if (res.code == '0') {
				alert("刷新成功！");
				window.location.reload();
			} else {
				alert(res.desc);
			}
		});
	}
}